<?php

include 'DBconn.php';

class GoodsReceivedMaster {

    public function getGRForCRDB($supplierFk)
	{
        $ret = array();
        $query = "Select * from  grn_header as gd LEFT JOIN grn_detail as gd ON gd.activestatus=1 and gd.gr_serialnumber_fk=gh.gr_serialnumber
		LEFT JOIN product_header as ph ON ph.activestatus=1 and ph.serialnumber=gd.pro_serialnumber_fk where gd.activestatus=1 and gh.gr_serialnumber_fk 
		IN(select gh.gr_serialnumber from grn_header as gh where gh.supplier_serialnumber_fk='$supplierFk' and gh.activestatus=1)";
        $s = mysql_query($query) or die(mysql_error());
        while ($row = mysql_fetch_object($s)) 
		{
            $temp = new VoGRHeaderList();
            $temp->gr_serialnumber = (int) $row->gr_serialnumber;
            $temp->gr_id = $row->gr_id;
			$temp->grn_detserialnumber = (int) $row->grn_detserialnumber;
			$temp->pro_serialnumber_fk = (int) $row->pro_serialnumber_fk;
			$temp->pro_name =  $row->pro_name;
			$temp->part_number =  $row->part_number;
			$temp->pro_description =  $row->pro_description;
			$temp->warehouse_serialnumber_fk = (int) $row->warehouse_serialnumber_fk;
            $temp->isSelected = false;
			$ret[] = $temp;
        }
        return $ret;
    }

    public function generateGrCode() {
        $query = "select gr_id from grn_header where activestatus=1 order by gr_serialnumber desc limit 1 ";
        $result = mysql_query($query) or die(mysql_error());
        if (mysql_num_rows($result)) {
            $row = mysql_fetch_array($result);
            $str = explode("GR", $row[0]);
            $num = intval($str[1]);
            $res = "GR" . ++$num;
        } else {
            $res = 'GR1001';
        }
        return $res;
    }

    public function addNewGR($VObject) 
	{
		$product_array = array();
		
        $grId = $this->generateGrCode();
		
        $query = "insert into grn_header (gr_id,supplier_serialnumber_fk,total_qty,total_amount,gr_gst,currency_serialnumber_fk,current_rate,vendor_ref_no,goods_received_date,cre_userid_fk,cre_datetime,activestatus) values ('$grId','$VObject->supplier_serialnumber_fk','$VObject->total_qty','$VObject->total_amount','$VObject->gr_gst','$VObject->currency_serialnumber_fk','$VObject->current_rate','$VObject->vendor_ref_no','$VObject->goods_received_date','$VObject->cre_userid_fk','$VObject->cre_datetime',1)";
        $s = mysql_query($query) or die(mysql_error());
        if ($s == 1) 
		{   
			$last_id = mysql_insert_id();
			
            foreach ($VObject->po_detserialnumber_fk as $r => $po_detserialnumber_fk) 
			{
				$pro_serialnumber_fk = mysql_real_escape_string($VObject->pro_serialnumber_fk[$r]);
				$po_qty = mysql_real_escape_string($VObject->po_qty[$r]);
                $received_qty = mysql_real_escape_string($VObject->received_qty[$r]);
				$unit_price = mysql_real_escape_string($VObject->unit_price[$r]);
				$pro_total_price = mysql_real_escape_string($VObject->pro_total_price[$r]);
                $warehouse_serialnumber_fk = mysql_real_escape_string($VObject->warehouse_serialnumber_fk[$r]);
				
                $product_array = $this->checkProductStatus($po_detserialnumber_fk,$pro_serialnumber_fk);
				
                if ($product_array[1] != "FULLY SATISFIED") 
				{					
                    $query1 = "insert into grn_detail(gr_serialnumber_fk,po_detserialnumber_fk,pro_serialnumber_fk,po_qty,received_qty,unit_price,pro_total_price,warehouse_serialnumber_fk,cre_userid_fk,cre_datetime,activestatus)values('$last_id','$po_detserialnumber_fk','$pro_serialnumber_fk','$po_qty','$received_qty','$unit_price','$pro_total_price','$warehouse_serialnumber_fk','$VObject->cre_userid_fk','$VObject->cre_datetime',1)";
                    $s1 = mysql_query($query1) or die(mysql_error());
					$gr_det_last_id = mysql_insert_id();
					
                    $product_array = $this->checkProductStatus($po_detserialnumber_fk,$pro_serialnumber_fk);
					
                    $query2 = "update po_details set pro_status='$product_array[1]' where po_detserialnumber='$po_detserialnumber_fk' and 
					pro_serialnumber_fk='$pro_serialnumber_fk' and activestatus=1";
                    mysql_query($query2) or die(mysql_error());
					
					$this->updateStockAfterGR($gr_det_last_id,$po_detserialnumber_fk,$pro_serialnumber_fk,$received_qty,$warehouse_serialnumber_fk);
					
					$trans_status = $this->checkPOStatus($po_detserialnumber_fk);
					
					$VObjectTR = new VoTrasactionMaster();
					$VObjectTR->gr_do_detserialnumber_fk = $gr_det_last_id;
					$VObjectTR->po_sc_detserialnumber_fk = $po_detserialnumber_fk;
					$VObjectTR->pro_serialnumber_fk = $pro_serialnumber_fk;
					$VObjectTR->trans_pro_qty = $received_qty;
					$VObjectTR->pro_status = $product_array[1];
					$VObjectTR->trans_status = $trans_status;
					$VObjectTR->warehouse_serialnumber_fk = $warehouse_serialnumber_fk;
					$VObjectTR->trans_type = 1;
					$VObjectTR->trans_date = $VObject->goods_received_date;
					$VObjectTR->cre_userid_fk = $VObject->cre_userid_fk;
					$VObjectTR->cre_datetime = $VObject->cre_datetime;
					$this->addNewTransactionIn($VObjectTR);
                }
            }
        }
		return $grId;
    }

	public function editGR($VObject) 
	{
        $query = "update grn_header set supplier_serialnumber_fk='$VObject->supplier_serialnumber_fk',total_qty='$VObject->total_qty',total_amount='$VObject->total_amount',gr_gst='$VObject->gr_gst',currency_serialnumber_fk='$VObject->currency_serialnumber_fk',current_rate='$VObject->current_rate',vendor_ref_no='$VObject->vendor_ref_no',goods_received_date='$VObject->goods_received_date' where gr_serialnumber='$VObject->gr_serialnumber' and  activestatus=1";
        $s = mysql_query($query) or die(mysql_error());
        if ($s) 
		{			
			$query2 = "Select grn_detserialnumber,po_detserialnumber_fk,pro_serialnumber_fk,received_qty,warehouse_serialnumber_fk from grn_detail where activestatus=1 and gr_serialnumber_fk='$VObject->gr_serialnumber'";
            $result2 = mysql_query($query2) or die(mysql_error());
			while($row2 = mysql_fetch_object($result2)) 
			 {
				$e_grn_detserialnumber = (int)$row2->grn_detserialnumber;
				$e_po_detserialnumber = (int)$row2->po_detserialnumber_fk;
				$e_pro_serialnumber_fk = (int)$row2->pro_serialnumber_fk;
				$e_received_qty = (int)$row2->received_qty;
				$e_warehouse_serialnumber_fk = (int)$row2->warehouse_serialnumber_fk;
				$this->updateStockBeforeEditGR($e_grn_detserialnumber,$e_po_detserialnumber,$e_pro_serialnumber_fk,$e_received_qty,$e_warehouse_serialnumber_fk);
				$this->deleteTransactionIn($e_grn_detserialnumber);
			 }
            mysql_query("update grn_detail set activestatus=0 where gr_serialnumber_fk='$VObject->gr_serialnumber'") or die(mysql_error());
			
           foreach ($VObject->po_detserialnumber_fk as $r => $po_detserialnumber_fk) 
			{
				$pro_serialnumber_fk = mysql_real_escape_string($VObject->pro_serialnumber_fk[$r]);
				$po_qty = mysql_real_escape_string($VObject->po_qty[$r]);
                $received_qty = mysql_real_escape_string($VObject->received_qty[$r]);
				$unit_price = mysql_real_escape_string($VObject->unit_price[$r]);
				$pro_total_price = mysql_real_escape_string($VObject->pro_total_price[$r]);
                $warehouse_serialnumber_fk = mysql_real_escape_string($VObject->warehouse_serialnumber_fk[$r]);
				
                $product_array = $this->checkProductStatus($po_detserialnumber_fk,$pro_serialnumber_fk);
				
                if ($product_array[1] != "FULLY SATISFIED") 
				{					
                    $query1 = "insert into grn_detail(gr_serialnumber_fk,po_detserialnumber_fk,pro_serialnumber_fk,po_qty,received_qty,unit_price,pro_total_price,warehouse_serialnumber_fk,cre_userid_fk,cre_datetime,activestatus)values('$VObject->gr_serialnumber','$po_detserialnumber_fk','$pro_serialnumber_fk','$po_qty','$received_qty','$unit_price','$pro_total_price','$warehouse_serialnumber_fk','$VObject->cre_userid_fk','$VObject->cre_datetime',1)";
                    $s1 = mysql_query($query1) or die(mysql_error());
					$gr_det_last_id = mysql_insert_id();
					
                    $product_array = $this->checkProductStatus($po_detserialnumber_fk,$pro_serialnumber_fk);
					
                    $query2 = "update po_details set pro_status='$product_array[1]' where po_detserialnumber='$po_detserialnumber_fk' and 
					pro_serialnumber_fk='$pro_serialnumber_fk' and activestatus=1";
                    mysql_query($query2) or die(mysql_error());
					
					$this->updateStockAfterGR($gr_det_last_id,$po_detserialnumber_fk,$pro_serialnumber_fk,$received_qty,$warehouse_serialnumber_fk);
					
					$trans_status = $this->checkPOStatus($po_detserialnumber_fk);
					
					$VObjectTR = new VoTrasactionMaster();
					$VObjectTR->gr_do_detserialnumber_fk = $gr_det_last_id;
					$VObjectTR->po_sc_detserialnumber_fk = $po_detserialnumber_fk;
					$VObjectTR->pro_serialnumber_fk = $pro_serialnumber_fk;
					$VObjectTR->trans_pro_qty = $received_qty;
					$VObjectTR->pro_status = $product_array[1];
					$VObjectTR->trans_status = $trans_status;
					$VObjectTR->warehouse_serialnumber_fk = $warehouse_serialnumber_fk;
					$VObjectTR->trans_type = 1;
					$VObjectTR->trans_date = $VObject->goods_received_date;
					$VObjectTR->cre_userid_fk = $VObject->cre_userid_fk;
					$VObjectTR->cre_datetime = $VObject->cre_datetime;
					$this->addNewTransactionIn($VObjectTR);
                }
            }
        }
    }
	
	public function addNewTransactionIn($VObjectTR) 
	{
        $query = "insert into stock_trans_details(gr_do_detserialnumber_fk,po_sc_detserialnumber_fk,pro_serialnumber_fk,trans_pro_qty,pro_status,trans_status,warehouse_serialnumber_fk,trans_type,trans_date,cre_userid_fk,cre_datetime,activestatus)values('$VObjectTR->gr_do_detserialnumber_fk','$VObjectTR->po_sc_detserialnumber_fk','$VObjectTR->pro_serialnumber_fk','$VObjectTR->trans_pro_qty','$VObjectTR->pro_status','$VObjectTR->trans_status','$VObjectTR->warehouse_serialnumber_fk','$VObjectTR->trans_type','$VObjectTR->trans_date','$VObjectTR->cre_userid_fk','$VObjectTR->cre_datetime',1)";
        $s = mysql_query($query) or die(mysql_error());
    }
	
    public function deleteTransactionIn($gr_do_detserialnumber_fk) 
	{
		//Function can be used to edit come delete
		
        $query = "update stock_trans_details set activestatus=0 where gr_do_detserialnumber_fk='$gr_do_detserialnumber_fk'  and trans_type='1'";
        $s = mysql_query($query) or die(mysql_error());
    }

	public function updateStockBeforeEditGR($grn_detserialnumber,$po_detserialnumber,$pro_serialnumber,$qty,$warehouse_serialnumber_fk) 
	{
		$query1 = "Select on_hand,on_sale,in_order,free_for_allocation from stock_details where trans_detserialnumber='$grn_detserialnumber' and trans_type=2 and activestatus=1";
        $s1 = mysql_query($query1) or die(mysql_error());
		$row1 = mysql_fetch_row($s1);
		$on_hand_det_qty = (int)$row1[0];
		$on_sale_det_qty = (int)$row1[1];
		$inorder_det_qty = (int)$row1[2];
		$free_allocation_det_qty = (int)$row1[3];
			
		$query2 = "select on_hand,in_order,on_sale,free_for_allocation from stock_header where pro_serialnumber='$pro_serialnumber' and warehouse_serialnumber_fk='$warehouse_serialnumber_fk' 
		and activestatus=1";
		$s2 = mysql_query($query2) or die(mysql_error());
		$row2 = mysql_fetch_row($s2);
		$on_hand = (int) $row2[0];
        $in_order = (int) $row2[1];
		$on_sale = (int) $row2[2];
		$free_for_allocation = (int) $row2[3];
			
		$on_hand = $on_hand - $on_hand_det_qty;
		$in_order = $in_order + $inorder_det_qty;
		$on_sale = $on_sale - $on_sale_det_qty;
		$free_for_allocation = $free_for_allocation + $free_allocation_det_qty;
				
		$query3 = "Update stock_header set on_hand='$on_hand',in_order='$in_order',on_sale='$on_sale',free_for_allocation='$free_for_allocation' where pro_serialnumber='$pro_serialnumber' and 
		warehouse_serialnumber_fk='$warehouse_serialnumber_fk' and activestatus=1";
		$s3 = mysql_query($query3) or die(mysql_error());		
		
		$query4 = "Update stock_details set activestatus=0 where trans_detserialnumber='$grn_detserialnumber' and trans_type=2";
		$s4 = mysql_query($query4) or die(mysql_error());
    }
	
    public function updateStockAfterGR($grn_detserialnumber,$po_detserialnumber,$pro_serialnumber,$gr_qty,$warehouse_serialnumber_fk) 
	{
		$inorder_det_qty = 0;
		$on_hand_det_qty = 0;
		$free_allocation_det_qty = 0;
		$on_sale_det_qty = 0;
		$balance_qty = 0;
		
		$query1 = "Select IFNULL(Sum(gd.received_qty),0) as received_qty,IFNULL((Select IFNULL(Sum(pms.allocated_qty),0) from po_map_sc as pms where pms.activestatus=1 and pms.self_sc=0 and pms.po_detserialnumber='$po_detserialnumber' GROUP BY pms.po_detserialnumber ORDER BY pms.po_detserialnumber),0) as allocated_qty  
		from grn_detail as gd where gd.activestatus=1 and gd.grn_detserialnumber!='$grn_detserialnumber' and gd.po_detserialnumber_fk='$po_detserialnumber' HAVING allocated_qty > 0";
        $s1 = mysql_query($query1) or die(mysql_error());
		$row1 = mysql_fetch_row($s1);
		$received_qty = (int) $row1[0];
		$allocated_qty = (int) $row1[1];
		if($allocated_qty <= $received_qty)
		{
			$balance_qty = 0;
		}else
		{
			$balance_qty = $allocated_qty - $received_qty;
		}
		//echo "balance_qty".$balance_qty."</br>";
		
        $query2 = "select on_hand,in_order,on_sale,free_for_allocation from stock_header where pro_serialnumber='$pro_serialnumber' and warehouse_serialnumber_fk='$warehouse_serialnumber_fk' 
		and activestatus=1";
        $s2 = mysql_query($query2) or die(mysql_error());
        $row2 = mysql_fetch_row($s2);
		$on_hand = (int) $row2[0];
        $in_order = (int) $row2[1];
		$on_sale = (int) $row2[2];
		$free_for_allocation = (int) $row2[3];
			
		//echo "on_hand".$on_hand."</br>";
		//echo "in_order".$in_order."</br>";
		//echo "on_sale".$on_sale."</br>";
		//echo "free_for_allocation".$free_for_allocation."</br>";
		
		$pre_inorder_qty = $in_order;
		$in_order = $in_order - $gr_qty;
		$post_inorder_qty = $in_order;
		
		$inorder_det_qty  = $pre_inorder_qty - $post_inorder_qty;

		if($balance_qty >= $gr_qty)
		{
			//echo "bon_sale".$on_sale."</br>";
			$pre_on_sale_qty = $on_sale;
			$on_sale = $on_sale + $gr_qty;
			$post_on_sale_qty = $on_sale;
			$on_sale_det_qty = $post_on_sale_qty - $pre_on_sale_qty;
			//echo "aon_sale".$on_sale."</br>";
		}else if($balance_qty < $gr_qty)
		{
		   $pre_on_sale_qty = $on_sale;
		   $on_sale = $on_sale + $balance_qty;
		   $post_on_sale_qty = $on_sale;
		   $on_sale_det_qty = $post_on_sale_qty - $pre_on_sale_qty;
				
		   $pre_on_hand_qty = $on_hand;
		   $on_hand = $on_hand + ($gr_qty - $balance_qty);
		   $post_on_hand_qty = $on_hand;
		   $on_hand_det_qty = $post_on_hand_qty - $pre_on_hand_qty;
			   
		   $pre_free_allocation_qty = $free_for_allocation;
		   $free_for_allocation = $free_for_allocation - ($gr_qty - $balance_qty);
		   $post_free_allocation_qty = $free_for_allocation;
		   $free_allocation_det_qty = $pre_free_allocation_qty - $post_free_allocation_qty;
		}
			
		//echo "fon_sale".$on_sale."</br>";
		//echo "fin_order".$in_order."</br>";
		//echo "ffree_for_allocation".$free_for_allocation."</br>";
		
		$query3 = "update stock_header set on_hand='$on_hand',in_order='$in_order',on_sale='$on_sale',free_for_allocation='$free_for_allocation' where pro_serialnumber='$pro_serialnumber' and 
		warehouse_serialnumber_fk='$warehouse_serialnumber_fk' and activestatus=1";
        $s3 = mysql_query($query3) or die(mysql_error());
			
        $query4 = "Insert Into stock_details(warehouse_serialnumber_fk,trans_detserialnumber,trans_type,pro_serialnumber,on_hand,on_sale,in_order,sold,free_for_allocation,activestatus) values('$warehouse_serialnumber_fk','$grn_detserialnumber',2,'$pro_serialnumber','$on_hand_det_qty','$on_sale_det_qty','$inorder_det_qty',0,'$free_allocation_det_qty',1)";
        $s4 = mysql_query($query4) or die(mysql_error());
    }

    public function deleteGR($VObject) 
	{
		$query1 = "Update grn_header set gr_id=concat('Del-',gr_id),cre_userid_fk='$VObject->cre_userid_fk',cre_datetime='$VObject->cre_datetime',activestatus=0 where gr_serialnumber='$VObject->gr_serialnumber'";
		mysql_query($query1) or die(mysql_error());
        $query2 = "Select grn_detserialnumber,po_detserialnumber_fk,pro_serialnumber_fk,received_qty,warehouse_serialnumber_fk from grn_detail where activestatus=1 and gr_serialnumber_fk='$VObject->gr_serialnumber'";
        $result2 = mysql_query($query2) or die(mysql_error());
		while($row2 = mysql_fetch_object($result2)) 
		{
			$e_grn_detserialnumber = (int)$row2->grn_detserialnumber;
			$e_po_detserialnumber = (int)$row2->po_detserialnumber_fk;
			$e_pro_serialnumber_fk = (int)$row2->pro_serialnumber_fk;
			$e_received_qty = (int)$row2->received_qty;
			$e_warehouse_serialnumber_fk = (int)$row2->warehouse_serialnumber_fk;
			$this->updateStockBeforeEditGR($e_grn_detserialnumber,$e_po_detserialnumber,$e_pro_serialnumber_fk,$e_received_qty,$e_warehouse_serialnumber_fk);
			$this->deleteTransactionIn($e_grn_detserialnumber);
		
			mysql_query("Update grn_detail set activestatus=0 where grn_detserialnumber='$row2->grn_detserialnumber'") or die(mysql_error());		
			
			$product_array = $this->checkProductStatus($e_po_detserialnumber,$e_pro_serialnumber_fk);
            $query2 = "update po_details set pro_status='$product_array[1]' where po_detserialnumber='$e_po_detserialnumber' and 
			pro_serialnumber_fk='$e_pro_serialnumber_fk' and activestatus=1";
            mysql_query($query2) or die(mysql_error());
			
			$e_po_serialnumber = 0;
			$res = mysql_query("Select po_serialnumber_fk from po_details where po_detserialnumber='$e_po_detserialnumber'") or die(mysql_error());
			while($row = mysql_fetch_object($res)){
				$e_po_serialnumber = (int)$row->po_serialnumber_fk;
			}
			$this->checkPOStatus($e_po_serialnumber);
		}
    }

    public function getAllGR() {
        $query = "Select gh.gr_serialnumber,gh.gr_id,gh.supplier_serialnumber_fk,gh.total_qty,gh.total_amount,gh.gr_gst,gh.currency_serialnumber_fk,gh.current_rate,gh.vendor_ref_no,gh.goods_received_date,gh.cre_userid_fk,gh.cre_datetime,gh.activestatus,ch.client_name from grn_header as gh 
		LEFT JOIN client_header as ch ON ch.activestatus=1 and ch.serialnumber=gh.supplier_serialnumber_fk where gh.activestatus=1";
        $s = mysql_query($query) or die(mysql_error());
        $ret = array();
        while ($row = mysql_fetch_object($s)) {
            $temp = new VoGoodsReceived();
            $temp->gr_serialnumber = (int)$row->gr_serialnumber;
            $temp->gr_id = $row->gr_id;
            $temp->supplier_serialnumber_fk = (int) $row->supplier_serialnumber_fk;
			$temp->total_qty = (int) $row->total_qty;
			$temp->total_amount = (double) $row->total_amount;
			$temp->gr_gst = (double) $row->gr_gst;
			$temp->currency_serialnumber_fk = (int) $row->currency_serialnumber_fk;
			$temp->current_rate = (double) $row->current_rate;
            $temp->vendor_ref_no = $row->vendor_ref_no;
            $temp->goods_received_date = date("d-m-Y", strtotime($row->goods_received_date));
            $temp->cre_userid_fk = (int) $row->cre_userid_fk;
            $temp->cre_datetime = $row->cre_datetime;
            $temp->activestatus = (boolean) $row->activestatus;
			$temp->client_name = $row->client_name;
			
			$result = mysql_query("Select * from gr_do where activestatus=1 and grn_detserialnumber_fk IN(Select grn_detserialnumber from grn_detail where activestatus=1 and gr_serialnumber_fk='$row->gr_serialnumber')") or die(mysql_error());
			if(mysql_num_rows($result)!=0){$temp->map_do = 1;}else{$temp->map_do = 0;}

            $query2 = "Select gd.gr_serialnumber_fk,gd.grn_detserialnumber,gd.po_detserialnumber_fk,pd.sc_detserialnumber_fk,ph.po_serialnumber,ph.po_id,gd.pro_serialnumber_fk,pro.pro_name,pro.part_number,pro.pro_description,
			pd.pro_qty,gd.received_qty,gd.warehouse_serialnumber_fk,gd.unit_price,gd.pro_total_price,(Select (Select pod.pro_qty from po_details as pod where pod.activestatus=1 and pod.po_detserialnumber=grnd.po_detserialnumber_fk) - IFNULL(Sum(grnd.received_qty),0) 
			From grn_detail as grnd where grnd.activestatus=1 and grnd.po_detserialnumber_fk=gd.po_detserialnumber_fk and grnd.gr_serialnumber_fk!=$row->gr_serialnumber) as balance_qty from grn_detail as gd LEFT JOIN po_details as pd ON pd.activestatus=1 and
			pd.po_detserialnumber=gd.po_detserialnumber_fk LEFT JOIN po_header as ph ON ph.activestatus=1 and ph.po_serialnumber=pd.po_serialnumber_fk LEFT JOIN product_header as pro ON pro.serialnumber=gd.pro_serialnumber_fk and pro.activestatus=1 where gd.activestatus=1 
			and gd.gr_serialnumber_fk='$row->gr_serialnumber'";
            $s2 = mysql_query($query2) or die(mysql_error());
            while ($row2 = mysql_fetch_object($s2)) {
                $temp->gr_serialnumber_fk[] = (int) $row2->gr_serialnumber_fk;
                $temp->grn_detserialnumber[] = (int) $row2->grn_detserialnumber;
                $temp->po_detserialnumber_fk[] = (int) $row2->po_detserialnumber_fk;
				$temp->sc_detserialnumber_fk[] = (int) $row2->sc_detserialnumber_fk;
				$temp->po_serialnumber[] = (int) $row2->po_serialnumber;
				$temp->po_id[] = $row2->po_id;
                $temp->pro_serialnumber_fk[] = (int) $row2->pro_serialnumber_fk;
				$temp->pro_name[] = $row2->pro_name;
				$temp->part_number[] = $row2->part_number;
				$temp->pro_description[] = $row2->pro_description;
                $temp->po_qty[] = (double) $row2->pro_qty;
                $temp->received_qty[] = (int) $row2->received_qty;		
				$temp->unit_price[] = (double) $row2->unit_price;
				$temp->pro_total_price[] = (double) $row2->pro_total_price;
				$temp->balance_qty[] = (int) $row2->balance_qty;
                $temp->warehouse_serialnumber_fk[] = (int) $row2->warehouse_serialnumber_fk;
            }
            $ret[] = $temp;
            mysql_free_result($s2);
        }
        mysql_free_result($s);
        return $ret;
    }
	
	public function getGRByCode($gr_serialnumber) {
        $query = "Select gh.gr_serialnumber,gh.gr_id,gh.supplier_serialnumber_fk,gh.total_qty,gh.total_amount,gh.gr_gst,gh.currency_serialnumber_fk,gh.current_rate,gh.vendor_ref_no,gh.goods_received_date,gh.cre_userid_fk,gh.cre_datetime,gh.activestatus,ch.client_name from grn_header as gh 
		LEFT JOIN client_header as ch ON ch.activestatus=1 and ch.serialnumber=gh.supplier_serialnumber_fk where gh.activestatus=1 and gh.gr_serialnumber='$gr_serialnumber'";
        $s = mysql_query($query) or die(mysql_error());
        $temp = new VoGoodsReceived();
        while ($row = mysql_fetch_object($s)) 
		{
            $temp->gr_serialnumber = (int) $row->gr_serialnumber;
            $temp->gr_id = $row->gr_id;
            $temp->supplier_serialnumber_fk = (int) $row->supplier_serialnumber_fk;
            $temp->vendor_ref_no = $row->vendor_ref_no;
			$temp->total_qty = (int) $row->total_qty;
			$temp->total_amount = (double) $row->total_amount;
			$temp->gr_gst = (double) $row->gr_gst;
			$temp->currency_serialnumber_fk = (int) $row->currency_serialnumber_fk;
			$temp->current_rate = (double) $row->current_rate;
            $temp->goods_received_date = date("d-m-Y", strtotime($row->goods_received_date));
            $temp->cre_userid_fk = (int) $row->cre_userid_fk;
            $temp->cre_datetime = $row->cre_datetime;
            $temp->activestatus = (boolean) $row->activestatus;
			$temp->client_name = $row->client_name;
			
			$result = mysql_query("Select * from gr_do where activestatus=1 and grn_detserialnumber_fk IN(Select grn_detserialnumber from grn_detail where activestatus=1 and gr_serialnumber_fk='$row->gr_serialnumber')") or die(mysql_error());
			if(mysql_num_rows($result)!=0){$temp->map_do = 1;}else{$temp->map_do = 0;}

            $query2 = "Select gd.gr_serialnumber_fk,gd.grn_detserialnumber,gd.po_detserialnumber_fk,pd.sc_detserialnumber_fk,ph.po_serialnumber,ph.po_id,gd.pro_serialnumber_fk,pro.pro_name,pro.part_number,pro.pro_description,
			pd.pro_qty,gd.received_qty,gd.warehouse_serialnumber_fk,gd.unit_price,gd.pro_total_price,(Select (Select pod.pro_qty from po_details as pod where pod.activestatus=1 and pod.po_detserialnumber=grnd.po_detserialnumber_fk) - IFNULL(Sum(grnd.received_qty),0) 
			From grn_detail as grnd where grnd.activestatus=1 and grnd.po_detserialnumber_fk=gd.po_detserialnumber_fk and grnd.gr_serialnumber_fk!=$row->gr_serialnumber) as balance_qty from grn_detail as gd LEFT JOIN po_details as pd ON pd.activestatus=1 and
			pd.po_detserialnumber=gd.po_detserialnumber_fk LEFT JOIN po_header as ph ON ph.activestatus=1 and ph.po_serialnumber=pd.po_serialnumber_fk LEFT JOIN product_header as pro ON pro.serialnumber=gd.pro_serialnumber_fk and pro.activestatus=1 where gd.activestatus=1 
			and gd.gr_serialnumber_fk='$row->gr_serialnumber'";
            $s2 = mysql_query($query2) or die(mysql_error());
            while ($row2 = mysql_fetch_object($s2)) {
                $temp->gr_serialnumber_fk[] = (int) $row2->gr_serialnumber_fk;
                $temp->grn_detserialnumber[] = (int) $row2->grn_detserialnumber;
                $temp->po_detserialnumber_fk[] = (int) $row2->po_detserialnumber_fk;
				$temp->sc_detserialnumber_fk[] = (int) $row2->sc_detserialnumber_fk;
				$temp->po_serialnumber[] = (int) $row2->po_serialnumber;
				$temp->po_id[] = $row2->po_id;
                $temp->pro_serialnumber_fk[] = (int) $row2->pro_serialnumber_fk;
				$temp->pro_name[] = $row2->pro_name;
				$temp->part_number[] = $row2->part_number;
				$temp->pro_description[] = $row2->pro_description;
                $temp->po_qty[] = (double) $row2->pro_qty;
                $temp->received_qty[] = (int) $row2->received_qty;
				$temp->unit_price[] = (double) $row2->unit_price;
				$temp->pro_total_price[] = (double) $row2->pro_total_price;
				$temp->balance_qty[] = (int) $row2->balance_qty;
                $temp->warehouse_serialnumber_fk[] = (int) $row2->warehouse_serialnumber_fk;
            }
        }
        return $temp;
    }

    public function getAllTransactions() {
        $ret = array();
        $query = "select * from stock_trans_details where activestatus=1";
        $s = mysql_query($query) or die(mysql_error());
        while ($row = mysql_fetch_object($s)) {
            $temp = new VoTrasactionMaster();
			$temp->serialnumber = (int) $row->serialnumber;
            $temp->gr_do_detserialnumber_fk = (int) $row->gr_do_detserialnumber_fk;
            $temp->po_sc_detserialnumber_fk = (int) $row->po_sc_detserialnumber_fk;
			$temp->pro_serialnumber_fk = (int) $row->pro_serialnumber_fk;
			$temp->trans_pro_qty = (int) $row->trans_pro_qty;
			$temp->pro_status = $row->pro_status;
            $temp->trans_status = $row->trans_status;
			$temp->warehouse_serialnumber_fk = (int) $row->warehouse_serialnumber_fk;
			$temp->trans_type = $row->trans_type;
            $temp->trans_date = $row->trans_date;
            $temp->cre_userid_fk = (int) $row->cre_userid_fk;
            $temp->cre_datetime = $row->cre_datetime;
            $temp->activestatus = (boolean) $row->activestatus;
			$ret[] = $temp;
        }
        mysql_free_result($s);
        return $ret;
    }

    public function checkPOStatus($po_detserialnumber_fk) 
	{
        $num_rows = 0;
        $status_rows = 0;
        $status_rows_part = 0;
        $po_status = "";
		
		$result = mysql_query("Select po_serialnumber_fk from po_details where activestatus=1 and po_detserialnumber='$po_detserialnumber_fk'") or die(mysql_error());
		$row = mysql_fetch_row($result);
		$po_serialnumber = $row[0];
		
        $query = "Select pro_status from po_details where po_serialnumber_fk='$po_serialnumber' and activestatus=1";
        $s2 = mysql_query($query) or die(mysql_error());
        $num_rows = mysql_num_rows($s2);
        while ($row2 = mysql_fetch_object($s2)) 
		{
            if ($row2->pro_status == "FULLY SATISFIED")
                $status_rows = $status_rows + 1;
            else if ($row2->pro_status == "PARTIALLY SATISFIED")
                $status_rows_part = $status_rows_part + 1;
        }
        if ($num_rows == $status_rows && $status_rows_part == 0)
            $po_status = "FULLY SATISFIED";
        else if (($status_rows_part > 0 || $status_rows > 0 ) && $num_rows != $status_rows)
            $po_status = "PARTIALLY SATISFIED";
        else if ($status_rows == 0 && $status_rows_part == 0)
            $po_status = "CONFIRM";
			
        $query2 = "update po_header set po_status='$po_status' where po_serialnumber='$po_serialnumber' and activestatus=1";
        $s2 = mysql_query($query2) or die(mysql_error());
		
        return $po_status;
    }

    public function checkProductStatus($po_detserialnumber_fk,$pro_serialnumber_fk) 
	{
        $received_qty = 0;
        $pro_status = "";
        $po_qty = 0;
		
        $product_array = array();
		
        $query2 = "Select received_qty,po_qty from grn_detail where po_detserialnumber_fk='$po_detserialnumber_fk' and 
		pro_serialnumber_fk='$pro_serialnumber_fk' and activestatus=1";
        $s2 = mysql_query($query2) or die(mysql_error());
        if(mysql_num_rows($s2) > 0) 
		{
            while($row = mysql_fetch_object($s2)) 
			{
                $received_qty+=$row->received_qty;
                $po_qty = $row->po_qty;
            }
			
            $product_array[0] = $received_qty;
			
            if ($product_array[0] < $po_qty)
                $product_array[1] = "PARTIALLY SATISFIED";
            else if ($product_array[0] >= $po_qty)
                $product_array[1] = "FULLY SATISFIED";
        }else{
            $product_array[0] = 0;
            $product_array[1] = "OPEN";
        }
        return $product_array;
    }
public function getPOListforGR($cus_fk)
{
$ret = array();
$s=mysql_query("select po_serialnumber,po_id,po_confirm_date,expected_datetime,po_remarks from po_header where supplier_serialnumber_fk='$cus_fk' and activestatus=1 and po_status!='FULLY SATISFIED' and po_status!='DRAFT'");
 
        while ($row = mysql_fetch_object($s)) {
            $temp = new VoGrPoList();
			$temp->po_confirm_date =$row->po_confirm_date;
            $temp->expected_datetime =$row->expected_datetime;
            $temp->po_remarks =$row->po_remarks;
			$temp->po_serialnumber = (int) $row->po_serialnumber;
            $temp->po_id =$row->po_id;
			//$temp->po_gst =$row->po_gst;
			$ret[] = $temp;
        }
	return $ret;	
}
}


class VoGrPoList
{
	var $po_serialnumber;
	var $po_id;
	var $po_confirm_date;
	var $expected_datetime;
	var $po_remarks;
	//var $po_gst;
}
class VoGRHeaderList 
{		
    var $gr_serialnumber;
    var $gr_id;
    var $grn_detserialnumber;
    var $pro_serialnumber_fk;
	var $pro_name;
	var $part_number;
	var $pro_description;
    var $warehouse_serialnumber_fk;
    var $isSelected;
}

class VoGoodsReceived {
    var $gr_serialnumber;
    var $gr_id;
    var $supplier_serialnumber_fk;
	var $total_amount;
	var $total_qty;
	var $gr_gst;
    var $currency_serialnumber_fk;
    var $current_rate;
    var $client_name;
    var $vendor_ref_no;
    var $goods_received_date;
    var $map_do;
    var $cre_userid_fk;
    var $cre_datetime;
    var $activestatus;
	var $po_id;
    var $po_serialnumber;
    var $po_detserialnumber_fk;
    var $grn_detserialnumber;
    var $pro_serialnumber_fk;
    var $po_qty;
    var $received_qty;
	var $unit_price;
    var $pro_total_price;
    var $warehouse_serialnumber_fk; 
}

class VoTrasactionMaster {      
    var $serialnumber;
	var $gr_do_detserialnumber_fk;
	var $po_sc_detserialnumber_fk;
	var $pro_serialnumber_fk;
	var $trans_pro_qty;
	var $pro_status;
    var $trans_status;
	var $warehouse_serialnumber_fk;
    var $trans_type;
    var $trans_date;
    var $cre_userid_fk;
    var $cre_datetime;
    var $activestatus;
}
//$ss=new GoodsReceivedMaster;
//echo $ss->updateStockAfterGR(1,1,1,1,1);
?>